package com.penghaonan.appframework.utils;

import android.os.Looper;
import android.text.TextUtils;
import android.util.Log;

import com.penghaonan.appframework.AppDelegate;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;

public class DebugUtils {
    private static final String TAG = "DebugUtils";

    private static boolean isDebug() {
        return AppDelegate.isDebug();
    }

    public static void assertNotPossible(String msg) {
        if (isDebug()) {
            throw new RuntimeException(msg);
        } else {
            Log.e(TAG, msg);
        }
    }

    public static void assertNotPossible(String msg, Throwable e) {
        if (isDebug()) {
            throw new RuntimeException(msg, e);
        } else {
            Log.e(TAG, msg, e);
        }
    }

    public static void assertTrue(boolean value) {
        if (isDebug() && !value) {
            throw new RuntimeException("value should be true");
        }
    }

    public static void assertTrue(boolean value, String msg) {
        if (isDebug() && !value) {
            throw new RuntimeException(msg);
        }
    }


    public static void assertFalse(boolean value) {
        if (isDebug() && value) {
            throw new RuntimeException("value should be false");
        }
    }

    public static void assertFalse(boolean value, String msg) {
        if (isDebug() && value) {
            throw new RuntimeException(msg);
        }
    }


    public static void assertMainThread() {
        if (isDebug() && !isMainThread()) {
            throw new IllegalThreadStateException("Invoke on main thread only!");
        }
    }

    public static void checkMainThread() {
        if (!isMainThread()) {
            throw new IllegalThreadStateException("Invoke on main thread only!");
        }
    }

    public static void assertNotMainThread() {
        if (isDebug() && isMainThread()) {
            throw new IllegalThreadStateException("CAN NOT invoke on main thread!");
        }
    }

    public static void checkNotMainThread() {
        if (isMainThread()) {
            throw new IllegalThreadStateException("CAN NOT invoke on main thread!");
        }
    }

    public static void assertNotNull(Object obj) {
        if (isDebug() && obj == null) {
            throw new NullPointerException("obj不能为null");
        }
    }

    public static void assertNotNull(Object obj, String msg) {
        if (isDebug() && obj == null) {
            throw new NullPointerException(msg);
        }
    }

    public static void assertNull(Object obj) {
        if (isDebug() && obj != null) {
            throw new NullPointerException("obj不为null:" + obj);
        }
    }


    public static void checkNotNull(Object obj) {
        if (obj == null) {
            throw new NullPointerException("obj不能为null");
        }
    }

    public static void checkNotNull(Object obj, String msg) {
        if (obj == null) {
            throw new NullPointerException(msg);
        }
    }


    public static void assertNotEmpty(Collection<?> collection) {
        if (isDebug()) {
            if (collection == null || collection.isEmpty()) {
                throw new IllegalArgumentException("collection不能为空");
            }
        }
    }

    public static void checkNotEmpty(Collection<?> collection) {
        if (collection == null || collection.isEmpty()) {
            throw new IllegalArgumentException("collection不能为空");
        }
    }

    public static void assertNotEmpty(String str) {
        if (isDebug() && TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("str不能为空");
        }
    }

    public static void checkNotEmpty(String str) {
        if (TextUtils.isEmpty(str)) {
            throw new IllegalArgumentException("str不能为空");
        }
    }

    public static void assertNotEmpty(Map<?, ?> map) {
        if (isDebug()) {
            if (map == null || map.isEmpty()) {
                throw new IllegalArgumentException("map不能为空");
            }
        }
    }

    public static void checkNotEmpty(Map<?, ?> map) {
        if (map == null || map.isEmpty()) {
            throw new IllegalArgumentException("map不能为空");
        }
    }

    public static boolean isMainThread() {
        return Looper.getMainLooper() == Looper.myLooper();
    }

    public static String throwable2String(final Throwable tr) {
        try {
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            tr.printStackTrace(pw);
            return sw.toString();
        } catch (Throwable e) {
            // 可能是oom了，do nothing
        }

        return "No Memory, throwable2String failed";
    }

    public static void tryCatch(Runnable runnable) {
        try {
            runnable.run();
        } catch (Exception e) {
            Logger.e(e);
            if (isDebug()) {
                throw e;
            }
        }
    }
}
